package com.valuegroup.smarttodo.common;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

public abstract class BaseRecyclerAdapter<T> extends RecyclerView.Adapter<BaseViewHolder> {
    private Context mContext;
    private LayoutInflater mInflater;
    // 数据
    private List<T> mData;
    // 布局
    private int mLayoutId;
    // 多布局支持
    private MultiTypeSupport multiType;

    public Context getmContext() {
        return mContext;
    }

    public BaseRecyclerAdapter(Context context, List<T> data, int layoutId) {
        this.mContext = context;
        this.mInflater = LayoutInflater.from(context);
        this.mData = new ArrayList<>();
        this.mData.addAll(data);
        this.mLayoutId = layoutId;
        this.multiType = new MultiTypeSupport() {
            @Override
            public int getLayoutId(List data, int position) {
                return mLayoutId;
            }

            @Override
            public int getAppendHeadLayoutSize() {
                return 0;
            }

            @Override
            public int getAppendBottomLayoutSize() {
                return 0;
            }

            @Override
            public boolean isBodyLayout(int dataSize, int position) {
                return true;
            }
        };
    }

    public BaseRecyclerAdapter(Context context, List<T> data, MultiTypeSupport multiType) {
        this(context, data, -1);
        this.multiType = multiType;
    }

    @NonNull
    @Override
    public BaseViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (multiType != null) {
            mLayoutId = viewType;
        }

        View v = mInflater.inflate(mLayoutId, parent, false);
        return new BaseViewHolder(v,mLayoutId);
    }

    @Override
    public void onBindViewHolder(@NonNull BaseViewHolder holder, int position) {
        // 设置点击和长按事件
        if (mItemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mItemClickListener.onItemClick(position);
                }
            });
        }
        if (mLongClickListener != null) {
            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    return mLongClickListener.onLongClick(position);
                }
            });
        }

        if (multiType.isBodyLayout(mData.size(), position)) {
            if (mData == null || mData.size() == 0 || (position - multiType.getAppendHeadLayoutSize()) >= mData.size()) {
                bindView(holder, null, position);
            } else {
                bindView(holder, mData.get(position - multiType.getAppendHeadLayoutSize()), position);
            }
        }

    }

    /**
     * 利用抽象方法回传出去，每个不一样的Adapter去设置
     */
    public abstract void bindView(BaseViewHolder v, T data, int position);

    @Override
    public int getItemCount() {
        return mData.size() + multiType.getAppendHeadLayoutSize() + multiType.getAppendBottomLayoutSize();
    }

    public int getDataSize() {
        return mData.size();
    }



    @Override
    public int getItemViewType(int position) {
        // 多布局
        if (multiType != null) {
            return multiType.getLayoutId(mData, position);
        }
        return super.getItemViewType(position);
    }

    public void addItem(int position, T item) {
        mData.add(position, item);
        notifyItemInserted(position + multiType.getAppendHeadLayoutSize());
    }

    public void addItem(T item) {
        addItem(getDataSize(), item);
    }

    // 刷新列表数据
    public void refreshListData(List<T> datas) {
        int previousSize = mData.size();
        mData.clear();
        notifyItemRangeRemoved(0, previousSize + multiType.getAppendHeadLayoutSize() + +multiType.getAppendBottomLayoutSize());
        mData.addAll(datas);
        notifyItemRangeInserted(0, datas.size() + multiType.getAppendHeadLayoutSize() + +multiType.getAppendBottomLayoutSize());
    }

    public void refreshData(int position, T data) {
        mData.set(position - multiType.getAppendHeadLayoutSize(), data);
        notifyItemChanged(position, data);
    }

    // 设置条目点击和长按事件
    private OnItemClickListener mItemClickListener;
    private OnLongClickListener mLongClickListener;

    public void setOnItemClickListener(OnItemClickListener itemClickListener) {
        this.mItemClickListener = itemClickListener;
    }

    public void setOnLongClickListener(OnLongClickListener longClickListener) {
        this.mLongClickListener = longClickListener;
    }


    public MultiTypeSupport getMultiType() {
        return multiType;
    }

    public interface OnItemClickListener {
        void onItemClick(int positon);
    }

    public interface OnLongClickListener {
        boolean onLongClick(int position);
    }

    //多布局接口
    public interface MultiTypeSupport<T> {
        int getLayoutId(List<T> data, int position);

        int getAppendHeadLayoutSize();

        int getAppendBottomLayoutSize();

        boolean isBodyLayout(int dataSize, int position);
    }
}
